package com.jason.library.widget.span

import android.graphics.Canvas
import android.graphics.Paint
import android.text.style.ReplacementSpan
import android.view.View
import android.view.ViewGroup

/**
 * @description: ViewSpan
 *
 * @author ChenBaoyang
 * @date 6/7/21 10:38
 */
class ViewSpan(private var view: View) : ReplacementSpan() {


    init {
        this.view.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
    }

    override fun getSize(
        paint: Paint,
        text: CharSequence?,
        start: Int,
        end: Int,
        fm: Paint.FontMetricsInt?
    ): Int {

        val widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
        val heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)

        view.measure(widthSpec, heightSpec)
        view.layout(0, 0, view.measuredWidth, view.measuredHeight)

        if (fm !=null) {
            val height = view.measuredHeight
            fm.top = -height /2
            fm.ascent = fm.top
            fm.bottom = height /2
            fm.descent = fm.bottom

        }

        return view.getRight();

    }

    override fun draw(
        canvas: Canvas,
        text: CharSequence?,
        start: Int,
        end: Int,
        x: Float,
        top: Int,
        y: Int,
        bottom: Int,
        paint: Paint
    ) {
        TODO("Not yet implemented")
    }
}